#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>

const int iter = 1000000000;
const int qntThreads = 1;
const int MAX_R = 32767;//maximo que o rand gera

typedef struct args args;
struct args
{
	int iter;
	int qntInside;//quantidade de pontos dentro do circulo
	int id;//id da thread
	long long next;//semente do rand
};

int randi(long long * next) 
{
    (*next) = (*next) * 1103515245 + 12345;
    return((unsigned)((*next)/65536) % 32768);
}

double doubleRandi(long long *next)
{
	return (double)randi(next)/MAX_R;
}

void * solve(void * param)
{

	args * xarg = (args *) param;

	int i;
	for (i = 0; i < xarg->iter; ++i)
	{
		double x = doubleRandi(&(xarg->next));
		double y = doubleRandi(&(xarg->next));
		

		double dis = x * x + y * y;

		if (dis <= 1) //se o ponto ta dentro do circulo, conta + 1
		{
			xarg->qntInside++;
		}
	}

}

int main(void)
{
	args vargs[qntThreads + 1];
	pthread_t threads[qntThreads + 1];
	int i;
	int inside = 0;


	for (i = 0; i < qntThreads; ++i)
	{
		vargs[i].iter = (iter + qntThreads - 1)/qntThreads;
		vargs[i].qntInside = 0;
		vargs[i].id = i;
		vargs[i].next = (i + 100);
		pthread_create( &threads[i], NULL, solve, (void*)(&vargs[i]));
	}

	for (i = 0; i < qntThreads; ++i)
	{
		pthread_join(threads[i], NULL);
		inside += vargs[i].qntInside;
	}

	printf("Com %d threads pi = %.6lf\n",qntThreads,4.0*(double)inside/iter);

	return 0;
}